<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>08_猜数字练习</title>
</head>
<body>
    <h2>已生成一个1~100之间的随机数:</h2>
    <input type="text" placeholder="请输入你猜的数字">
    <button onclick="guess()">点我验证</button>
    <h2>小提示:<span></span></h2>
    <script>
        let num=parseFloat(Math.random()*100+1);
        console.log(num)
        var count=0;

        function guess(){
            let num2=document.querySelector('input').value;
            if(!/^\d+$/.test(num)){
                alert('请输入数字!');
                return;
            }
            let span=document.querySelector('span');
            num=parseFloat(num);
            num2=parseFloat(num2);
            count++;
            if (num2<num){
                span.innerHTML='猜小了';
            }else {
                span.innerHTML='猜大了';
            }
            switch (num2){
                case num:
                    span.innerHTML='恭喜你用了'+count+'次猜对了';
                default:
                    span.innerHTML='猜错了';

            }

        }
    </script>
</body>
</html>